package com.you.modules.websocket.controller;

import com.you.common.constant.Constant;
import com.you.common.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.simp.SimpMessagingTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/web/websocket")
public class WebSocketController {

    @Autowired
    private SimpMessagingTemplate simpMessagingTemplate;

    @RequestMapping("/index")
    public String index() {
        return "websocket/index.html";
    }

    @GetMapping("/send")
    public void sendMsg(@RequestParam("msg") String msg){
        simpMessagingTemplate.convertAndSend(Constant.WebSocket.TOPIC_GREET, ResponseUtil.success(msg));
    }
}
